Array.prototype.random = function () { return this[Math.floor((Math.random()*this.length))]; } const TIER1 = ['US', 'CA', 'NZ', 'AU', 'UK']; const TIER3 = ['IN','CN','ID','PK','CG','CD', 'BD', 'PH', 'EG', 'BR']; //default smartlink in case country can't be resolved const fallBackSmartlink = 'https://google.com/'; const offers = { 'https://google.com/': [...TIER1, 'DE'], //all tier1 countries + germany 'https://bing.com/': ['LT', 'EE', 'LV'], //just all baltic states 'https://duckduckgo.com/': [...TIER3, 'UK'], //all tier3 countries + United Kingdom }; async function handleRequest(request) { let redirectTo = fallBackSmartlink; let country = request.cf ? request.cf.country : '404'; let links = []; for(const offer in offers){ if( offers[offer].includes(country) ){ links.push(offer); } } if( links.length ){ redirectTo = links.random(); } //variant #1, redirect to smartlink without referrer //html becauce we want privacy and keep referrer hidden const html = `Loading..
Got to registration
`; return new Response(html, { headers: { 'content-type': 'text/html;charset=UTF-8', }, }); //variant #2, redirect to smartlink keeping refferer //return Response.redirect(redirectTo, 301); //variant #3, serve offer from iframe so no redirects happens //By default this variant hides referrer as well. to enable it, remove referrerpolicy from iframe tag. /*const html ` Onboarding - Create account `;*/ } addEventListener("fetch", (event) => { event.respondWith( handleRequest(event.request).catch( (err) => new Response(err.stack, { status: 500 }) ) ); });